<template>
    <Header></Header>
    <Banner :isIndex="false" @changeCourse="changeCourse" :bannerList="banner_list" :courseList="course_list">
        <template #search>
            <el-input
                    v-model="searchParam"
                    clearable
                    class="w-50 m-2"
                    placeholder="请输入搜索内容"
                    :suffix-icon="Search"
                    @input="searchContent"
            />
        </template>
    </Banner>
    <div class="question-body">
        <div class="breadcrumb">
            <el-breadcrumb :separator-icon="'ArrowRightBold'">
                <el-breadcrumb-item class="link" @click="$router.push({name:'index'})">首页</el-breadcrumb-item>
                <el-breadcrumb-item>综合搜索</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <el-row justify="space-between">
            <el-col :span="4">
                <el-menu
                        active-text-color="white"
                        background-color="rgba(235, 244, 255, 1)"
                        class="question"
                        :default-active="currentItem"
                        text-color="rgba(102, 102, 102, 1)"
                        @select="handleSelect"
                >
                    <el-menu-item index="course">
                        <el-icon><setting /></el-icon>
                        <span>视频</span>
                    </el-menu-item>
                    <el-menu-item index="question">
                        <el-icon><setting /></el-icon>
                        <span>题目</span>
                    </el-menu-item>
                    <el-menu-item index="information">
                        <el-icon><setting /></el-icon>
                        <span>资讯</span>
                    </el-menu-item>
                </el-menu>
            </el-col>
            <el-col :span="18">
<!--                {{currentItem}}-->
                <component :is="currentItem" :searchParam="search" :currentItem="currentItem" :subject_id="activeSubjectId" :subject_name="activeSubjectName"></component>
            </el-col>
        </el-row>
    </div>
    <staticFooter></staticFooter>
</template>

<script>
    import staticFooter from "../../components/staticFooter";
    import {debounce} from "../../utils/debounce/debounce";
    import { Search } from '@element-plus/icons-vue'
    import course from "./tabs/course";
    import question from "./tabs/question";
    import information from "./tabs/information";
    import Banner from "@/components/Banner";
    import Header from "@/components/Header";
    import {useRoute, useRouter} from "vue-router";
    import {bannerList,courseList,recentLatelyLearn} from "@/api/api";
    import {reactive,ref,onMounted ,toRefs} from 'vue';
    export default {
        name: "index",
        setup(){

            let data = reactive({
                searchParam:'',
                search:''
            })

            let router = useRouter();
            let route = useRoute();
            //右侧动态组件
            let currentItem = ref('course');

            //当前课程
            let activeSubjectId = ref(0)
            let activeSubjectName = ref('')
            let activeName = ref('')
            let active_map = {'课程':'course','题目':'question','咨讯':'information'};
            let activeName_map = {'course':'课程','question':'题目','information':'咨讯'}
            currentItem.value = route.params.activeName;
            activeName.value = route.params.activeName;
            activeSubjectId.value = localStorage.getItem('activeCourse')
            activeSubjectName.value = localStorage.getItem('activeCourseName')

            //banner图集合
            let banner_list = ref([])
            let getBannerList = () =>{
                bannerList({id:activeSubjectId.value,type:1}).then(res =>{
                    console.log('轮播图列表',res)
                    banner_list.value = res.data
                })
            }
            getBannerList();

            //课程列表
            let course_list = reactive([]);
            const getCourseList = () =>{
                courseList().then(res =>{
                    console.log('课程列表',res)
                    course_list.push(...res.data)
                    let acoutCourse = window.localStorage.getItem('activeCourse') ? window.localStorage.getItem('activeCourse'):course_list[0].courseID
                    window.localStorage.setItem('activeCourse',acoutCourse);
                })
            }
            getCourseList();

            //切换课程
            let changeCourse = (activeCourse) => {
                let activeCourseName = course_list.filter(item => item.courseID == activeCourse.courseId)[0].name;
                console.log('activeCourseName',activeCourseName)
                window.localStorage.setItem('activeCourseName', String(activeCourseName));
                activeSubjectId.value = localStorage.getItem('activeCourse')
                activeSubjectName.value = localStorage.getItem('activeCourseName')
                getBannerList();
            }

            //左侧menu
            const handleSelect = (key,keyPath) => {
                currentItem.value = key
                activeName.value = activeName_map[key]
            }

            let searchContent = debounce(() =>{
                data.search = data.searchParam
            },500)

            onMounted(() =>{
                // let current = localStorage.getItem('currentItem')
                // console.log('current',current)
                // if(current != 'undefined' || current != 'null'){
                //   console.log('current',current)
                //   currentItem.value = current
                // }else{
                //   currentItem.value = 'myCourse'
                // }
            })
            return{
                ...toRefs(data),
                searchContent,
                Search,
                changeCourse,
                course_list,
                activeName,
                handleSelect,
                currentItem,
                activeSubjectId,
                activeSubjectName,
                banner_list,
            }
        },
        components:{
            Header,
            Banner,
            course,
            question,
            information,
            staticFooter
        }
    }
</script>

<style>
    .el-menu-item.is-active{
        background-color: rgba(42, 130, 228, 1);
    }
</style>

<style scoped>
    .link{
        cursor: pointer;
    }
    .link :hover{
        color: #409eff;
    }
    .question-body{
        width: 1200px;
        margin: 0 auto;
        margin-bottom: 60px;
    }
    .question-body .breadcrumb{
        margin: 20px 0px;
    }
</style>
